-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (31 loc) · 959 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Node {
int data;
Node left, right;
public Node(int value) {
data = value;
left = right = null;
}
}
public class Main {
static boolean isBSTUtil(Node root, int min, int max) {
if (root == null)
return true;
if (root.data <= min || root.data >= max)
return false;
return isBSTUtil(root.left, min, root.data) && isBSTUtil(root.right, root.data, max);
}
static boolean isBST(Node root) {
return isBSTUtil(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(20);
root.left.left = new Node(3);
root.left.right = new Node(7);
if (isBST(root))
System.out.println("The tree is a valid BST.");
else
System.out.println("The tree is not a valid BST.");
}
}